//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package swing;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import main.Language;
import oldClasses.DadesTotal;
import database.DataBaseAdapter;
import database.DataSet;
import database.DataTable;


//Ventana principal de la aplicacion
public class Frame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	public static DadesTotal totalDades = new DadesTotal();
	private static String baseActual = "";
	private static String urlActual = "";
	private static String passActual = "";
	private static String userActual = "";

	//Con estas dos variables, frame podra interactuar con la base de datos
	private DataBaseAdapter dataBaseAdapter;
	private DataSet dataSet;

	

	private JButton importarJButton = null; // @jve:decl-index=0:
	private JButton updateDataBaseJButton;

	private Frame myframe;
	private Map<String, String> translations;
	private TabPanel tabbedPane;
	

	/**
	 * This is the default constructor
	 */
	public Frame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(685, 419);
		this.setJMenuBar(getJJMenuBar());

		dataBaseAdapter = DataBaseAdapter.getSingleton();
		dataBaseAdapter
				.setParams(urlActual, baseActual, userActual, passActual);

		translations = Language.getSingleton().getTranslation();
		initializeDataSet();

		tabbedPane = new TabPanel(dataSet);
		
		getJJMenuBar().add(new JButton("Obrir Base"));
		getJJMenuBar().add(getImportarJButton("Importar arxiu"));

		updateDataBaseJButton = new JButton(translations.get("Update dataBase"));

		updateDataBaseJButton.addMouseListener(new MouseListener() {
			//En este evento del mouse se actualiza la base de datos, para ello se recorren 
			//las diferentes dataTables y se ejecuta el metodo "executeSqlCommand" de cada
			//dataTable
						@Override
			public void mouseClicked(MouseEvent arg0) {
				//Recuperamos la posicion del tab que tiene el focus
				int selectedTab = tabbedPane.getSelectedIndex();
				ArrayList<DataTable> dataTableCollection = dataSet
						.getDataTableCollection();
				Iterator<DataTable> iteratorDataTableCollection = dataTableCollection
						.iterator();
				while (iteratorDataTableCollection.hasNext()) {
					DataTable dataTable = iteratorDataTableCollection.next();
					dataTable.executeSqlCommand();
				}
				initializeDataSet();
				tabbedPane = new TabPanel(dataSet);
				//Posicionamos el focus en el tab que lo tenia al principio
				tabbedPane.setSelectedIndex(selectedTab);
				setContentPane(tabbedPane);				
			}
			
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {

			}

			@Override
			public void mouseExited(MouseEvent arg0) {

			}

			@Override
			public void mouseEntered(MouseEvent arg0) {

			}
		});

		getJJMenuBar().add(updateDataBaseJButton);

		setContentPane(tabbedPane);
		setTitle("JFrame");
		myframe = this;
		
	}

	public DataSet getDataSet() {
		return dataSet;
	}
	
	private void initializeDataSet() {
		dataBaseAdapter.openBBDD();
		dataSet = new DataSet();
		String dataBaseName = dataBaseAdapter.getDbName();
		LinkedList<String> tableNames = dataBaseAdapter.getTablesFromDataBase();
		Iterator<String> iteratorTableNames = tableNames.iterator();

		while (iteratorTableNames.hasNext()) {
			String tableName = iteratorTableNames.next();
			DataTable table = new DataTable(tableName, dataBaseName);
			dataSet.addTable(table);
		}
		dataBaseAdapter.close();

	}

	public JButton getImportarJButton(String nom) {
		if (importarJButton == null) {
			importarJButton = new JButton(nom);
			importarJButton.addMouseListener(new MouseListener() {

				public void mouseClicked(java.awt.event.MouseEvent e) {
					FrameImportar fimport = new FrameImportar(myframe);
					fimport.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					fimport.setVisible(true);

				}

				public void mouseEntered(MouseEvent e) {
				}

				public void mouseExited(MouseEvent e) {
				}

				public void mousePressed(MouseEvent e) {
				}

				public void mouseReleased(MouseEvent e) {
				}
			});
			return importarJButton;
		} else
			return importarJButton;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar
	 * 
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
		}
		return jJMenuBar;
	}

	public static String getUrlActual() {
		return urlActual;
	}

	public static void setUrlActual(String urlActual) {
		Frame.urlActual = urlActual;
	}

	public static String getBaseActual() {
		return baseActual;
	}

	public static void setBaseActual(String baseActual) {
		Frame.baseActual = baseActual;
	}

	public static String getPassActual() {
		return passActual;
	}

	public static void setPassActual(String passActual) {
		Frame.passActual = passActual;
	}

	public static String getUserActual() {
		return userActual;
	}

	public static void setUserActual(String userActual) {
		Frame.userActual = userActual;
	}
	/*
	 * public void loadXLSFile(){ File arrel= new File("C:/"); fileChooser
	 * chooser = new fileChooser(arrel) ; chooser.setFileFilter(new
	 * FileFilterXLS()); chooser.setDialogType(JFileChooser.OPEN_DIALOG); final
	 * JDialog dialogAbrir = chooser.createDialog(this);
	 * dialogAbrir.setVisible(true); chooser.addActionListener(new
	 * AbstractAction() { private static final long serialVersionUID = 1L;
	 * 
	 * public void actionPerformed(ActionEvent evt) { if
	 * (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) { // Open
	 * or Save was clicked
	 * 
	 * // Hide dialog
	 * 
	 * dialogAbrir.setVisible(false);
	 * //importarJButton.setText(chooser.getSelectedFile().getAbsolutePath());
	 * 
	 * 
	 * } else if (JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand()))
	 * { // Cancel was clicked
	 * 
	 * // Hide dialog dialogAbrir.setVisible(false); } }
	 * 
	 * }); }
	 */
} // @jve:decl-index=0:visual-constraint="127,112"
